<template>
  <el-card class="app-container">
    <div v-if="!detailsShow">
      <div class="itemTitle">页面标题</div>
      <div class="search">
        <el-form :inline="true" :model="queryParams" ref="queryForm">
          <el-col :span="5">
            <el-form-item label="考核任务" prop="examineTask">
              <el-input v-model="queryParams.examineTask" placeholder="请输入考核任务" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="被考核人" prop="assessee">
              <el-input v-model="queryParams.assessee" placeholder="请输入被考核人" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="考核分类" prop="examineType">
              <el-select v-model="queryParams.examineType" placeholder="请选择考核分类" clearable>
                <el-option v-for="dict in examineTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="截至填报时间" prop="expirationTime">
              <el-date-picker clearable v-model="queryParams.expirationTime" type="date" value-format="yyyy-MM-dd"
                placeholder="选择截至填报时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="状态" prop="state">
              <el-input v-model="queryParams.state" placeholder="请输入状态" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <el-button type="primary" @click="handleQuery">搜索</el-button>
              <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </div>
      <div class="content">
        <div class="ct-btns">
          <el-button type="primary" @click="handleAdd" v-hasPermi="['examine:task:add']">新增</el-button>
        </div>
        <div class="ct-table">
          <elTables :table-data="tableData" :columns="tableColumns" :totalLength="total" :currentPage="queryParams.pageNum"
            :handle-current-change="handleCurrentChange" isPagin :curHeight="tableHeight" />
        </div>
      </div>
    </div>
    <Detail v-if="detailsShow" :isEdit="false" :info="form" :title="detailTitle" @close="closeDetailPage" type="fill"/>
  </el-card>
</template>

<script>
const defaultQueryParams = {
  pageNum: 1,
  pageSize: 10,
};
import { pageTask,getTask, delTask, addTask, updateTask } from "@/api/examine/task";
import Detail from '../components/details.vue'
export default {
  name: "Task",
  components:{ Detail },
  data() {
    return {
      detailTitle:'',
      tableHeight: 0,
      isEdit: false,
      detailsShow:false,
      dialogFormVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      tableData: [],
      // 查询参数
      examineTypeOptions: [],
      queryParams: {
        ...defaultQueryParams,
        examineTask: null,
        assessee: null,
        examineType: null,
        expirationTime: null,
        factoryId: null,
        state: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      tableColumns: [
        { key: "examineTask", title: "考核任务" },
        { key: "examineCategory", title: "考核类别" },
        { key: "examineDepartment", title: "考核部门" },
        { key: "personResponsible", title: "责任人" },
        { key: "assessee", title: "被考核人" },
        { key: "examineType", title: "考核分类",render:(h,params) => {
          return h('div',{},this.examineTypeOptions.find(_ => _.dictValue == params.row.examineType)?.dictLabel)
        }},
        { key: "expirationTime", title: "截至填报时间" },
        { key: "score", title: "总分" },
        { key: "state", title: "状态",render:(h,params) => {
          return h('div',{},params.row.state=='2'?'已填报':'未填报')
        }},
        { key: "expireState", title: "是否逾期",render:(h,params) => {
          return h('div',{},params.row.expireState=='2'?'未逾期':'已逾期')
        }},
        {
          title: "操作",
          align: "center",
          fixed: 'right',
          width: 260,
          render: (h, params) => {
            return h("div", [
            h(
              "el-button",
              {
                props: {
                  type: "text",
                  size: "small",
                  disabled:params.row.state=='1',
                },
                directives: [
                  {
                    name: "hasPermi",
                    value: ["examine:task:info"],
                  },
                ],
                on: {
                  click: () => {
                    this.handleUpdate(params.row, 1);
                  },
                },
              },
              "填报"
            ),
            h(
                "el-button",
                {
                  props: {
                    type: "text",
                    size: "small",
                  },
                  directives: [
                    {
                      name: "hasPermi",
                      value: ["examine:task:info"],
                    },
                  ],
                  on: {
                    click: () => {
                      this.handleUpdate(params.row, 2)
                    },
                  },
                },
                "查看"
              ),
              h(
                "el-button",
                {
                  props: {
                    type: "text",
                    size: "small",
                  },
                  directives: [
                    {
                      name: "hasPermi",
                      value: ["examine:task:remove"],
                    },
                  ],
                  on: {
                    click: () => {
                      this.handleDelete(params.row)
                    },
                  },
                },
                "删除"
              ),
            ]);
          },
        },
      ]

    };
  },
  created() {
    this.getPage();
    this.getDicts("examine_type").then(response => {
      this.examineTypeOptions = response.data;
    });
  },
  mounted() {
    this.$nextTick(() => {
      this.getTableHeight();
    })
  },
  methods: {
    changeRowStatus(row){
      console.log('row',row);
      let msg='';
      let data={
        ...row
      };
      if(row.state=='1'){
        data.state='2';
        msg='确认下发 ' + row.examineTask + ' 考核任务的数据项?';
      }else{
        data.state='1';
        msg='确认取消下发 ' + row.examineTask + ' 考核任务的数据项?';
      }
      this.$confirm(msg, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return updateTask(data);
      }).then(() => {
        this.getPage();
        this.msgSuccess("操作成功！");
      })
    },
    getTableHeight() {
      let height = document.getElementsByClassName('ct-table')[0].clientHeight;
      this.tableHeight = height - 40;
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getPage();
    },
    /** 查询考核任务列表 */
    getPage() {
      this.loading = true;
      pageTask(this.queryParams).then(response => {
        this.tableData = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 考核分类字典翻译
    examineTypeFormat(row, column) {
      return this.selectDictLabel(this.examineTypeOptions, row.examineType);
    },
    // 取消按钮
    // 取消按钮
    cancel() {
      this.$refs.upload.$refs.uploadRef.clearFiles();
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        examineTask: null,
        examineDepartment: null,
        personResponsible: null,
        assessee: null,
        examineType: null,
        examinationTableId: null,
        expirationTime: null,
        illustrate: null,
        factoryId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        createDept: null,
        delFlag: null,
        state: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getPage();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isEdit = true;
      this.detailsShow=true;
      this.detailTitle = "添加考核任务";
    },
    closeDetailPage(){
      this.detailsShow=false;
      this.getPage();
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.detailsShow=true;
      if (type == 1) {
        this.isEdit = true;
        this.detailTitle='编辑考核任务';
      } else {
        this.isEdit = false;
        this.detailTitle='查看考核任务';
      }
      this.reset();
      const id = row.id || this.ids
      getTask(id).then(response => {
        this.form = response.data;   
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.$refs.upload.$refs.uploadRef.clearFiles();
              this.open = false;
              this.getPage();
            });
          } else {
            addTask(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.$refs.upload.$refs.uploadRef.clearFiles();
              this.open = false;
              this.getPage();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        let data
        if (Array.isArray(ids)) {
          data = ids
        } else {
          data = [ids]
        }
        return delTask(data);
      }).then(() => {
        this.getPage();
        this.msgSuccess("删除成功");
      })
    },
  }
};
</script>
<style lang="scss" scoped></style>